﻿#include "FITKAcousticsNCTMgrAdaptor.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsNCT.h"
#include "FITKAcousticsHDF5Writer.h"
#include "FITKAcousticsHDF5Reader.h"
#include "FITK_Interface/FITKInterfaceIO/FITKHDF5FileTool.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTLayUp.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTUserDefined.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTMultipleNoise.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTLayUp.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTRegularTrimLayer.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTResistive.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTSeptum.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTPerforated.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTUserDefined.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTMultipleNoise.h"

bool IO::FITKAcousticsNCTMgrAdaptor::adaptR()
{
    Acoustics::FITKAcousticsNCTManager* NCTManager =
        dynamic_cast<Acoustics::FITKAcousticsNCTManager*>(_dataObj);
    if (NCTManager == nullptr || _reader == nullptr) return false;
    m_toolp = _reader->getHDF5FileTool();//初始化工具

    std::tuple<bool, H5::Group>Ret = m_toolp->openGp("NoiseControlTreatment");//打开声学包组
    if (std::get<0>(Ret) == false) { return false; }
    H5::Group SpectraGroup = std::get<1>(Ret);

    bool TreatmentLayUpsRet = this->readTreatmentLayUps(SpectraGroup, NCTManager);
    bool UserDefTreatmentsRet = this->readUserDefTreatments(SpectraGroup, NCTManager);
    bool MulitipleNCTsRet = this->readMulitipleNCTs(SpectraGroup, NCTManager);

    return TreatmentLayUpsRet && UserDefTreatmentsRet&&MulitipleNCTsRet;
}

bool IO::FITKAcousticsNCTMgrAdaptor::adaptW()
{
    Acoustics::FITKAcousticsNCTManager* NCTManager =
        dynamic_cast<Acoustics::FITKAcousticsNCTManager*>(_dataObj);
    if (NCTManager == nullptr || _writer == nullptr) return false;
    m_toolp = _writer->getHDF5FileTool();//初始化工具


    QList<Acoustics::FITKAcousticsAbsNCT*>TreatmentLayUps = NCTManager->getAcousticsNCTByType(Acoustics::FITKAcousticsAbsNCT::TreatmentLayUp);//多层声学包
    QList<Acoustics::FITKAcousticsAbsNCT*>UserDefTreatments = NCTManager->getAcousticsNCTByType(Acoustics::FITKAcousticsAbsNCT::UserDefTreatment);//用户自定义声学包
    QList<Acoustics::FITKAcousticsAbsNCT*>MulitipleNCTs = NCTManager->getAcousticsNCTByType(Acoustics::FITKAcousticsAbsNCT::MulitipleNCT);//多重声学包

    std::tuple<bool, H5::Group> Ret = m_toolp->createGp("NoiseControlTreatment");     //创建声学包组
    if (std::get<0>(Ret) == false) { return false; }
    H5::Group NCTGroup = std::get<1>(Ret);
    
    bool TreatmentLayUpsRet = writeTreatmentLayUps(NCTGroup, TreatmentLayUps);
    bool UserDefTreatmentsRet = writeUserDefTreatments(NCTGroup, UserDefTreatments);
    bool MulitipleNCTsRet = writeMulitipleNCTs(NCTGroup, MulitipleNCTs);
    return TreatmentLayUpsRet && UserDefTreatmentsRet&&MulitipleNCTsRet;
}

bool IO::FITKAcousticsNCTMgrAdaptor::readMulitipleNCTs(H5::Group & NCTGroup, Acoustics::FITKAcousticsNCTManager * mager)
{
    if (mager == nullptr)
    {
        return false;
    }


    std::tuple<bool, H5::Group>Ret = m_toolp->openGp(NCTGroup, "MulitipleNCTs");//打开组
    if (std::get<0>(Ret) == false) { return false; }
    H5::Group thisMaterialsGroup = std::get<1>(Ret);

    std::tuple<bool, H5::Attribute>RetAttr = m_toolp->openAttribut(thisMaterialsGroup, "MulitipleNCTsCount"); //打开组个数属性
    if (std::get<0>(RetAttr) == false) { return false; }
    H5::Attribute CountAttrattr = std::get<1>(RetAttr);

    int count = -1;
    CountAttrattr.read(H5::PredType::NATIVE_INT, &count);

    for (int i = 0; i < count; ++i)
    {
        //通过转换i为str，获取该str属性下对应组的名称(具体材料名称)
        std::string subGroupName = m_toolp->readGroupAttrName(thisMaterialsGroup, i);
        QString Name = QString::fromStdString(subGroupName);
        
        //打开子组
        std::tuple<bool, H5::Group>RetGroup = m_toolp->openGp(thisMaterialsGroup, Name.toStdString().data());
        if (std::get<0>(RetGroup) == false) { continue; }//打不开则退出此次
        H5::Group subGroup = std::get<1>(RetGroup);
        m_toolp->RestoreString(Name);

        int ID = -1;                        //获取数据ID
        std::vector<float>Color = {};       //表格颜色数据
        int DataCount = -1;//包层数
        m_toolp->ReadOneDArrayDataSetFloat(subGroup, "Color", Color);
        m_toolp->readGroupint(subGroup, "ID", &ID);
        m_toolp->readGroupint(subGroup, "DataCount", &DataCount);//读取包层数

        Acoustics::FITKAcousticsNCTMultipleNoise* newObjectP = new Acoustics::FITKAcousticsNCTMultipleNoise;
        if (newObjectP == nullptr)
        {
             continue;
        }
        if (Color.size() != 3) { continue; }
        newObjectP->setColor(Color.at(0), Color.at(1), Color.at(2));
        newObjectP->setDataObjectName(Name);

        for (int j = 0; j < DataCount; ++j)
        {
            QString lay = QString::number(j);

            std::tuple<bool, H5::Group>layRet = m_toolp->openGp(subGroup, lay.toStdString().data());
            if (std::get<0>(layRet) == false) { continue; }
            H5::Group layGroup = std::get<1>(layRet);

            int layID = -1;
            int NCTID = -1;
            double Coverage = 0.0;
            m_toolp->readGroupint(layGroup, "NCTID", &NCTID);
            m_toolp->readGroupint(layGroup, "layID", &layID);
            m_toolp->readGroupDouble(layGroup, "Coverage", &Coverage);

            Acoustics::FITKAcousticsNCTMultipleNoiseWeight *lobj = new Acoustics::FITKAcousticsNCTMultipleNoiseWeight;
            if (lobj==nullptr)
            {
                return false;
            }
            lobj->setNCTID(NCTID);
            lobj->setCoverage(Coverage);
            newObjectP->insertDataObj(j, lobj);


            Core::FITKAbstractDataIDAlter a;
            a.modifyDataID(lobj, layID);
        }

        Core::FITKAbstractDataIDAlter a;
        a.modifyDataID(newObjectP, ID);

        mager->appendDataObj(newObjectP);
    }
    return true;
}

bool IO::FITKAcousticsNCTMgrAdaptor::writeMulitipleNCTs(H5::Group & NCTGroup, QList<Acoustics::FITKAcousticsAbsNCT*> MulitipleNCTs)
{
    //创建的HDF5目录结构
    const char *Path = "NoiseControlTreatment/MulitipleNCTs";

    int count = MulitipleNCTs.size();//获取数据行数

    
    //在组下创建组，并为组附加数据个数属性
    m_toolp->CreateGroupAndAttr(NCTGroup, "MulitipleNCTs", "MulitipleNCTsCount", count);

    //获取刚刚创建的组
    H5::Group thisGroup = m_toolp->GetPathGroup(Path);
    for (int i = 0; i < count; ++i)
    {
        Acoustics::FITKAcousticsNCTMultipleNoise* ObjP = dynamic_cast<Acoustics::FITKAcousticsNCTMultipleNoise*>(MulitipleNCTs.at(i));
        if (ObjP == nullptr)
        {
            continue;
        }


        QString Name = ObjP->getDataObjectName();          //获取包名称
        m_toolp->CheckString(Name);                        //检查是否有特殊字符

        m_toolp->CreateNameFromAttr(thisGroup, i, Name.toStdString());

        std::tuple<bool, H5::Group>Ret = m_toolp->createGp(thisGroup, Name);//创建子节点
        if (std::get<0>(Ret) == false) { continue; }
        H5::Group subGroup = std::get<1>(Ret);

        float Color[3] = { 0.0,0.0,0.0 };
        ObjP->getColor(Color);                        //表格颜色数据
        int ID = ObjP->getDataObjectID();             //获取数据ID
        int DataCount = ObjP->getDataCount();//包层数
        m_toolp->CheckString(Name);
        m_toolp->writeGroupAttrInt(subGroup, "DataCount", DataCount);
        m_toolp->writeGroupAttrInt(subGroup, "ID", ID);
        m_toolp->writeGroupAttrStr(subGroup, "Name", Name.toStdString());
        m_toolp->CreateOneDArrayDataSetFloat(subGroup, 3, "Color", Color);

        
        for (int j = 0; j < DataCount; ++j)
        {
            Acoustics::FITKAcousticsNCTMultipleNoiseWeight *lobj = ObjP->getDataByIndex(j);
            if (lobj == nullptr)
            {
                continue;
            }
            int NCTID = lobj->getNCTID();//声包id
            double Coverage = lobj->getCoverage();//覆盖面积
            
            std::tuple<bool, H5::Group>Retj = m_toolp->createGp(subGroup, QString::number(j));//创建子节点
            if (std::get<0>(Retj) == false) { return false; }
            H5::Group subGroupj = std::get<1>(Retj);
            int layID = lobj->getDataObjectID();
            m_toolp->writeGroupAttrInt(subGroupj, "layID", layID);
            m_toolp->writeGroupAttrInt(subGroupj, "NCTID", NCTID);
            m_toolp->writeGroupAttrDouble(subGroupj, "Coverage", Coverage);
            Core::FITKAbstractDataIDAlter a;
            a.modifyDataID(lobj, ID);
        }

    }
    return true;
}

bool IO::FITKAcousticsNCTMgrAdaptor::readUserDefTreatments(H5::Group & NCTGroup, Acoustics::FITKAcousticsNCTManager * mager)
{
    if (mager==nullptr)
    {
        return false;
    }
    std::tuple<bool, H5::Group>Ret = m_toolp->openGp(NCTGroup, "UserDefTreatments");//打开组
    if (std::get<0>(Ret) == false) { return false; }
    H5::Group thisMaterialsGroup = std::get<1>(Ret);

    std::tuple<bool, H5::Attribute>RetAttr = m_toolp->openAttribut(thisMaterialsGroup, "UserDefTreatmentsCount"); //打开组个数属性
    if (std::get<0>(RetAttr) == false) { return false; }
    H5::Attribute CountAttrattr = std::get<1>(RetAttr);

    int count = -1;
    CountAttrattr.read(H5::PredType::NATIVE_INT, &count);

    for (int i = 0; i < count; ++i)
    {
        //通过转换i为str，获取该str属性下对应组的名称(具体材料名称)
        std::string subGroupName = m_toolp->readGroupAttrName(thisMaterialsGroup, i);
        QString Name = QString::fromStdString(subGroupName);
        
        //打开子组
        std::tuple<bool, H5::Group>RetGroup = m_toolp->openGp(thisMaterialsGroup, Name.toStdString().data());
        if (std::get<0>(RetGroup) == false) { continue; }//打不开则退出此次
        H5::Group subGroup = std::get<1>(RetGroup);
        m_toolp->RestoreString(Name);

        int ID = -1;                        //获取数据ID
        std::vector<float>Color = {};       //表格颜色数据
        int AbsorptionCoeffID = -1;//吸收系数ID
        int InsertionLossResonantID = -1;//插入丢失共振ID
        int InsertionLossNonResonantID = -1;//插入丢失非共振ID
        int DampingFlexureID = -1;//阻尼弯曲ID
        int DampingExtensionID = -1;//阻尼扩展ID
        int DampingShearID = -1;//阻尼剪切ID
        double MassPerUnitArea = 0.0;//单位面积质量



        m_toolp->ReadOneDArrayDataSetFloat(subGroup, "Color", Color);
        m_toolp->readGroupint(subGroup, "ID", &ID);
        m_toolp->readGroupint(subGroup, "AbsorptionCoeffID", &AbsorptionCoeffID);
        m_toolp->readGroupint(subGroup, "InsertionLossResonantID", &InsertionLossResonantID);
        m_toolp->readGroupint(subGroup, "InsertionLossNonResonantID", &InsertionLossNonResonantID);
        m_toolp->readGroupint(subGroup, "DampingFlexureID", &DampingFlexureID);
        m_toolp->readGroupint(subGroup, "DampingExtensionID", &DampingExtensionID);
        m_toolp->readGroupint(subGroup, "DampingShearID", &DampingShearID);
        m_toolp->readGroupDouble(subGroup, "MassPerUnitArea", &MassPerUnitArea);


        Acoustics::FITKAcousticsNCTUserDefined* NewObjP = new Acoustics::FITKAcousticsNCTUserDefined;
        if (NewObjP == nullptr)
        {
            return false;
        }
        NewObjP->setAbsorptionCoeffID(AbsorptionCoeffID);
        NewObjP->setInsertionLossResonantID(InsertionLossResonantID);
        NewObjP->setInsertionLossNonResonantID(InsertionLossNonResonantID);
        NewObjP->setDampingFlexureID(DampingFlexureID);
        NewObjP->setDampingExtensionID(DampingExtensionID);
        NewObjP->setDampingShearID(DampingShearID);
        NewObjP->setMassPerUnitArea(MassPerUnitArea);
        if (Color.size() != 3) { continue; }
        NewObjP->setColor(Color.at(0), Color.at(1), Color.at(2));
        NewObjP->setDataObjectName(Name);


        Core::FITKAbstractDataIDAlter a;
        a.modifyDataID(NewObjP, ID);
        mager->appendDataObj(NewObjP);
    }
    return true;
}

bool IO::FITKAcousticsNCTMgrAdaptor::writeUserDefTreatments(H5::Group & NCTGroup, QList<Acoustics::FITKAcousticsAbsNCT*> UserDefTreatments)
{
    //创建的HDF5目录结构
    const char *Path = "NoiseControlTreatment/UserDefTreatments";

    int count = UserDefTreatments.size();//获取数据行数


    //在组下创建组，并为组附加数据个数属性
    m_toolp->CreateGroupAndAttr(NCTGroup, "UserDefTreatments", "UserDefTreatmentsCount", count);

    //获取刚刚创建的组
    H5::Group thisGroup = m_toolp->GetPathGroup(Path);

    for (int i = 0; i < count; ++i)
    {
        Acoustics::FITKAcousticsNCTUserDefined* ObjP = dynamic_cast<Acoustics::FITKAcousticsNCTUserDefined*>(UserDefTreatments.at(i));
        if (ObjP == nullptr)
        {
            continue;
        }
        QString Name = ObjP->getDataObjectName();       //获取名称

        float Color[3] = { 0.0,0.0,0.0 };
        ObjP->getColor(Color);                        //表格颜色数据
        int ID = ObjP->getDataObjectID();             //获取数据ID

        int AbsorptionCoeffID = ObjP->getAbsorptionCoeffID();//吸收系数ID
        int InsertionLossResonantID = ObjP->getInsertionLossResonantID();//插入丢失共振ID
        int InsertionLossNonResonantID = ObjP->getInsertionLossNonResonantID();//插入丢失非共振ID
        int DampingFlexureID = ObjP->getDampingFlexureID();//阻尼弯曲ID
        int DampingExtensionID = ObjP->getDampingExtensionID();//阻尼扩展ID
        int DampingShearID = ObjP->getDampingShearID();//阻尼剪切ID
        double MassPerUnitArea = ObjP->getMassPerUnitArea();//单位面积质量


        //检查字符串输入是否有特殊字符
        m_toolp->CheckString(Name);

        //在组下创建具体材料属性,并绑定到组
        m_toolp->CreateNameFromAttr(thisGroup, i, Name.toStdString());

        std::tuple<bool, H5::Group>Ret = m_toolp->createGp(thisGroup, Name);//创建子节点
        if (std::get<0>(Ret) == false) { continue; }

        H5::Group subGroup = std::get<1>(Ret);

        std::vector<bool>writeResultBools;//存储此次写入时返回成功与否数据
        //将数据写入到具体属性中
        writeResultBools.push_back(m_toolp->writeGroupAttrStr(subGroup, "Name", Name.toStdString()));
        writeResultBools.push_back(m_toolp->CreateOneDArrayDataSetFloat(subGroup, 3, "Color", Color));
        writeResultBools.push_back(m_toolp->writeGroupAttrInt(subGroup, "ID", ID));
        writeResultBools.push_back(m_toolp->writeGroupAttrInt(subGroup, "AbsorptionCoeffID", AbsorptionCoeffID));
        writeResultBools.push_back(m_toolp->writeGroupAttrInt(subGroup, "InsertionLossResonantID", InsertionLossResonantID));
        writeResultBools.push_back(m_toolp->writeGroupAttrInt(subGroup, "InsertionLossNonResonantID", InsertionLossNonResonantID));
        writeResultBools.push_back(m_toolp->writeGroupAttrInt(subGroup, "DampingFlexureID", DampingFlexureID));
        writeResultBools.push_back(m_toolp->writeGroupAttrInt(subGroup, "DampingExtensionID", DampingExtensionID));
        writeResultBools.push_back(m_toolp->writeGroupAttrInt(subGroup, "DampingShearID", DampingShearID));
        writeResultBools.push_back(m_toolp->writeGroupAttrDouble(subGroup, "MassPerUnitArea", MassPerUnitArea));

        bool allBool = this->areAllTrue(writeResultBools);
        if (allBool == false)
        {
            //如果不是全部都为true则，写入失败，写该数据终止
            return false;
        }
    }
    return true;
}

bool IO::FITKAcousticsNCTMgrAdaptor::readTreatmentLayUps(H5::Group & NCTGroup, Acoustics::FITKAcousticsNCTManager * mager)
{
    if (mager==nullptr)
    {
        return false;
    }


    std::tuple<bool, H5::Group>Ret = m_toolp->openGp(NCTGroup, "TreatmentLayUps");//打开组
    if (std::get<0>(Ret) == false) { return false; }
    H5::Group thisMaterialsGroup = std::get<1>(Ret);

    std::tuple<bool, H5::Attribute>RetAttr = m_toolp->openAttribut(thisMaterialsGroup, "TreatmentLayUpsCount"); //打开组个数属性
    if (std::get<0>(RetAttr) == false) { return false; }
    H5::Attribute CountAttrattr = std::get<1>(RetAttr);

    int count = -1;
    CountAttrattr.read(H5::PredType::NATIVE_INT, &count);

    for (int i = 0; i < count; ++i)
    {
        //通过转换i为str，获取该str属性下对应组的名称(具体材料名称)
        std::string subGroupName = m_toolp->readGroupAttrName(thisMaterialsGroup, i);
        QString Name = QString::fromStdString(subGroupName);
        
        //打开子组
        std::tuple<bool, H5::Group>RetGroup = m_toolp->openGp(thisMaterialsGroup, Name.toStdString().data());
        if (std::get<0>(RetGroup) == false) { continue; }//打不开则退出此次
        H5::Group subGroup = std::get<1>(RetGroup);
        m_toolp->RestoreString(Name);
        double MaxFieldAngle = 0.0;         //最大仰角
        int ID = -1;                        //获取数据ID
        QString Describe;                   //该数据结构未有备注信息结构
        std::vector<float>Color = {};       //表格颜色数据

        m_toolp->readGroupDouble(subGroup, "MaxFieldAngle", &MaxFieldAngle);
        m_toolp->ReadOneDArrayDataSetFloat(subGroup, "Color", Color);
        m_toolp->readGroupint(subGroup, "ID", &ID);

        int DataCount = -1;
        m_toolp->readGroupint(subGroup, "DataCount", &DataCount);//读取包层数
        Acoustics::FITKAcousticsNCTLayUp* newObjectP = new Acoustics::FITKAcousticsNCTLayUp;
        if (newObjectP==nullptr)
        {
            continue;
        }

        //设置数据
        if (newObjectP == nullptr) { continue; }
        newObjectP->setDataObjectName(Name);
        if (Color.size() != 3) { continue; }
        newObjectP->setColor(Color.at(0), Color.at(1), Color.at(2));
        newObjectP->setMaxFieldAngle(MaxFieldAngle);

        for (int j = 0; j < DataCount; ++j)
        {
            QString lay = QString::number(j);

            std::tuple<bool, H5::Group>layRet = m_toolp->openGp(subGroup, lay.toStdString().data());
            if (std::get<0>(layRet) == false) { continue; }
            H5::Group layGroup = std::get<1>(layRet);

            std::string NctTypeStr{};
            m_toolp->readGroupStr(layGroup, "NctTypeStr", NctTypeStr);//获取该层类型

            Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType NctType = this->StrFromNCTLayUpType(QString::fromStdString(NctTypeStr));//转换为具体类型

            this->readTreatmentLayUpsLay(newObjectP, NctType, layGroup, j);

        }
        Core::FITKAbstractDataIDAlter a;
        a.modifyDataID(newObjectP, ID);
        mager->appendDataObj(newObjectP);
    }
    return false;
}

bool IO::FITKAcousticsNCTMgrAdaptor::readTreatmentLayUpsLay(Acoustics::FITKAcousticsNCTLayUp * newObjectP, Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType NctType, H5::Group & layGroup, int lay)
{
    if (newObjectP == nullptr)
    {
        return false;
    }

    Acoustics::FITKAcousticsNCTRegularTrimLayer* RegularTrimLayerP = nullptr;
    Acoustics::FITKAcousticsNCTSeptum* SeptumP = nullptr;
    Acoustics::FITKAcousticsNCTPerforated*PerforatedP = nullptr;
    Acoustics::FITKAcousticsNCTResistive* ResistiveP = nullptr;

    switch (NctType)
    {
    case Acoustics::FITKAcousticsNCTAbstractLayObj::None:
    {
        return false;
        break;
    }
    case Acoustics::FITKAcousticsNCTAbstractLayObj::Gap:  //缝隙  
    case Acoustics::FITKAcousticsNCTAbstractLayObj::Panel://实体板
    case Acoustics::FITKAcousticsNCTAbstractLayObj::Fiber://纤维
    case Acoustics::FITKAcousticsNCTAbstractLayObj::Foam: //泡沐
    {
        //缝隙 实体板 纤维 泡沫都使用一种类型
        RegularTrimLayerP = new Acoustics::FITKAcousticsNCTRegularTrimLayer;
        break;
    }
    case Acoustics::FITKAcousticsNCTAbstractLayObj::Septum://隔离层
    {
        SeptumP = new Acoustics::FITKAcousticsNCTSeptum;
        break;
    }
    case Acoustics::FITKAcousticsNCTAbstractLayObj::Perforated://多孔介质
    {
        PerforatedP = new Acoustics::FITKAcousticsNCTPerforated;
        break;
    }
    case Acoustics::FITKAcousticsNCTAbstractLayObj::Resistive://阻尼
    {
        ResistiveP = new Acoustics::FITKAcousticsNCTResistive;
        break;
    }
    default:
    {
        return false;
        break;
    }
    }

    if (RegularTrimLayerP != nullptr)//缝隙 实体板 纤维 泡沫
    {
        int FluidMaterialID = -1;//流体id
        int SolidMaterialID = -1;//固体id
        double Thickness = 0.0;//厚度
        double LossFactor = 0.0;//损耗系数
        int ID = -1;
        m_toolp->readGroupint(layGroup, "FluidMaterialID", &FluidMaterialID);
        m_toolp->readGroupint(layGroup, "ID", &ID);
        m_toolp->readGroupint(layGroup, "SolidMaterialID", &SolidMaterialID);
        m_toolp->readGroupDouble(layGroup, "Thickness", &Thickness);
        m_toolp->readGroupDouble(layGroup, "LossFactor", &LossFactor);

        RegularTrimLayerP->setFluidMaterialID(FluidMaterialID);
        RegularTrimLayerP->setSolidMaterialID(SolidMaterialID);
        RegularTrimLayerP->setThickness(Thickness);
        RegularTrimLayerP->setLossFactor(LossFactor);

        Core::FITKAbstractDataIDAlter a;
        a.modifyDataID(RegularTrimLayerP, ID);

        RegularTrimLayerP->initType();
        //newObjectP->appendDataObj(RegularTrimLayerP);
        newObjectP->insertDataObj(lay, RegularTrimLayerP);
        return true;
    }
    if (ResistiveP != nullptr)//阻尼
    {
        double Density = 0.0;//密度
        double FlowResistivity = 0.0;//流阻率
        int ID = -1;
        double Thickness = 0.0;
        m_toolp->readGroupDouble(layGroup, "Density", &Density);
        m_toolp->readGroupDouble(layGroup, "FlowResistivity", &FlowResistivity);
        m_toolp->readGroupDouble(layGroup, "Thickness", &Thickness);
        m_toolp->readGroupint(layGroup, "ID", &ID);

        ResistiveP->setDensity(Density);
        ResistiveP->setFlowResistivity(FlowResistivity);
        ResistiveP->setThickness(Thickness);

        Core::FITKAbstractDataIDAlter a;
        a.modifyDataID(ResistiveP, ID);
        newObjectP->insertDataObj(lay, ResistiveP);
        return true;
    }
    if (SeptumP != nullptr)//隔离板
    {
        double SurfaceMass = 0.0;//表面质量
        int ID = -1;
        double Thickness = 0.0;
        m_toolp->readGroupDouble(layGroup, "SurfaceMass", &SurfaceMass);
        m_toolp->readGroupDouble(layGroup, "Thickness", &Thickness);
        m_toolp->readGroupint(layGroup, "ID", &ID);

        SeptumP->setSurfaceMass(SurfaceMass);
        SeptumP->setThickness(Thickness);
        Core::FITKAbstractDataIDAlter a;
        a.modifyDataID(SeptumP, ID);
        newObjectP->insertDataObj(lay, SeptumP);
        return true;
    }
    if (PerforatedP != nullptr)//多孔介质
    {
        double Density = 0.0;//密度
        int fluidMaterialID = -1;//流体材料ID
        int MaterialID = -1;//材料ID
        double Porosity = 0.0;//孔隙度
        double Radius = 0.0;//半径
        double AreaRatio = 0.0;//面积比率
        double FlowResistivity = 0.0;//流量阻力
        std::string  SolidTypeStr = "";//固体类型(枚举)
        std::string PerforationTypeStr = "";//穿孔类型(枚举)
        double Thickness = 0.0;
        int ID = -1;

        Acoustics::FITKAcousticsNCTPerforated::NCTSolidType SolidType;
        Acoustics::FITKAcousticsNCTPerforated::PerforationType PerforationType;


        m_toolp->readGroupDouble(layGroup, "Density", &Density);
        m_toolp->readGroupint(layGroup, "fluidMaterialID", &fluidMaterialID);
        m_toolp->readGroupint(layGroup, "MaterialID", &MaterialID);
        m_toolp->readGroupDouble(layGroup, "Porosity", &Porosity);
        m_toolp->readGroupDouble(layGroup, "Radius", &Radius);
        m_toolp->readGroupDouble(layGroup, "AreaRatio", &AreaRatio);
        m_toolp->readGroupDouble(layGroup, "FlowResistivity", &FlowResistivity);
        m_toolp->readGroupDouble(layGroup, "Thickness", &Thickness);
        m_toolp->readGroupStr(layGroup, "SolidType", SolidTypeStr);
        m_toolp->readGroupStr(layGroup, "PerforationType", PerforationTypeStr);
        m_toolp->readGroupint(layGroup, "ID", &ID);
        //str转枚举
        SolidType = this->StrFromNCTSolidType(QString::fromStdString(SolidTypeStr));
        PerforationType = this->StrFromPerforation(QString::fromStdString(PerforationTypeStr));


        PerforatedP->setDensity(Density);
        PerforatedP->setfluidMaterialID(fluidMaterialID);
        PerforatedP->setMaterialID(MaterialID);
        PerforatedP->setPorosity(Porosity);
        PerforatedP->setRadius(Radius);
        PerforatedP->setAreaRatio(AreaRatio);
        PerforatedP->setFlowResistivity(FlowResistivity);
        PerforatedP->setSolidType(SolidType);
        PerforatedP->setPerforationType(PerforationType);
        PerforatedP->setThickness(Thickness);
        Core::FITKAbstractDataIDAlter a;
        a.modifyDataID(PerforatedP, ID);
        newObjectP->insertDataObj(lay, PerforatedP);
        return true;
    }

    return false;
}

bool IO::FITKAcousticsNCTMgrAdaptor::writeTreatmentLayUps(H5::Group & NCTGroup, QList<Acoustics::FITKAcousticsAbsNCT*> TreatmentLayUps)
{
    //创建的HDF5目录结构
    const char *Path = "NoiseControlTreatment/TreatmentLayUps";

    int count = TreatmentLayUps.size();//获取数据行数

    //在组下创建组，并为组附加数据个数属性
    m_toolp->CreateGroupAndAttr(NCTGroup, "TreatmentLayUps", "TreatmentLayUpsCount", count);

    //获取刚刚创建的组
    H5::Group thisGroup = m_toolp->GetPathGroup(Path);
    for (int i = 0; i < TreatmentLayUps.size(); ++i)//处理一个包
    {
        Acoustics::FITKAcousticsNCTLayUp* ObjP = dynamic_cast<Acoustics::FITKAcousticsNCTLayUp*>(TreatmentLayUps.at(i));
        if (ObjP == nullptr)
        {
            continue;
        }
        QString Name = ObjP->getDataObjectName();          //获取包名称
        m_toolp->CheckString(Name);                        //检查是否有特殊字符

        m_toolp->CreateNameFromAttr(thisGroup, i, Name.toStdString());

        std::tuple<bool, H5::Group>Ret = m_toolp->createGp(thisGroup, Name);//创建子节点
        if (std::get<0>(Ret) == false) { continue; }
        H5::Group subGroup = std::get<1>(Ret);

        double MaxFieldAngle = 0.0;         //最大仰角
        float Color[3] = { 0.0,0.0,0.0 };   //表格颜色数据
        int ID = -1;                        //获取数据ID
        QString Describe;                   //该数据结构未有备注信息结构

        std::vector<bool>writeResultBools;

        MaxFieldAngle = ObjP->getMaxFieldAngle();
        ObjP->getColor(Color);
        ID = ObjP->getDataObjectID();
        writeResultBools.push_back(m_toolp->writeGroupAttrDouble(subGroup, "MaxFieldAngle", MaxFieldAngle));
        writeResultBools.push_back(m_toolp->writeGroupAttrInt(subGroup, "ID", ID));
        writeResultBools.push_back(m_toolp->CreateOneDArrayDataSetFloat(subGroup, 3, "Color", Color));


        int DataCount = ObjP->getDataCount();              //包层数
        writeResultBools.push_back(m_toolp->writeGroupAttrInt(subGroup, "DataCount", DataCount));


        bool allBool = this->areAllTrue(writeResultBools);
        if (allBool == false)
        {
            //如果不是全部都为true则，写入失败，写该数据终止
            return false;
        }


        for (int j = 0; j < DataCount; ++j)
        {
            Acoustics::FITKAcousticsNCTAbstractLayObj* lobj = ObjP->getDataByIndex(j);//每层基类
            if (lobj == nullptr)
            {
                continue;
            }

            if (this->writewriteTreatmentLayUpsLay(subGroup, lobj, j) == false)//写一层数据 根据index 为一层id
            {
                continue;
            }
        }
    }
    return false;
}

bool IO::FITKAcousticsNCTMgrAdaptor::writewriteTreatmentLayUpsLay(H5::Group & group, Acoustics::FITKAcousticsNCTAbstractLayObj * lobj, int layer)
{
    if (lobj == nullptr)
    {
        return false;
    }
    QString lay = QString::number(layer);//int转换为string 写入hdf5

    //Gap 缝隙 Panel实体板  fiber纤维   foam泡沫 使用该类型
    Acoustics::FITKAcousticsNCTRegularTrimLayer* RegularTrimLayerP = nullptr;

    //阻尼使用该类型
    Acoustics::FITKAcousticsNCTResistive* ResistiveP = nullptr;

    //隔离板使用该类型
    Acoustics::FITKAcousticsNCTSeptum* SeptumP = nullptr;

    //多孔介质使用该类型
    Acoustics::FITKAcousticsNCTPerforated*PerforatedP = nullptr;
    Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType upType = lobj->getNCTLayUpType();//每层类型
    QString NctTypeStr = this->NCTLayUpTypeToStr(upType);//枚举转str

    std::tuple<bool, H5::Group>Ret = m_toolp->createGp(group, lay.toStdString().data());//创建子节点
    if (std::get<0>(Ret) == false) { return false; }
    H5::Group subGroup = std::get<1>(Ret);

    m_toolp->writeGroupAttrStr(subGroup, "NctTypeStr", NctTypeStr.toStdString());//存储该组类型

    switch (upType)
    {
    case Acoustics::FITKAcousticsNCTAbstractLayObj::None:
    {
        return false;
        break;
    }
    case Acoustics::FITKAcousticsNCTAbstractLayObj::Gap:  //缝隙  
    case Acoustics::FITKAcousticsNCTAbstractLayObj::Panel://实体板
    case Acoustics::FITKAcousticsNCTAbstractLayObj::Fiber://纤维
    case Acoustics::FITKAcousticsNCTAbstractLayObj::Foam: //泡沐
    {
        //缝隙 实体板 纤维 泡沫都使用一种类型
        RegularTrimLayerP = dynamic_cast<Acoustics::FITKAcousticsNCTRegularTrimLayer*>(lobj);
        break;
    }
    case Acoustics::FITKAcousticsNCTAbstractLayObj::Septum://隔离层
    {
        SeptumP = dynamic_cast<Acoustics::FITKAcousticsNCTSeptum*>(lobj);
        break;
    }
    case Acoustics::FITKAcousticsNCTAbstractLayObj::Perforated://多孔介质
    {
        PerforatedP = dynamic_cast<Acoustics::FITKAcousticsNCTPerforated*>(lobj);
        break;
    }
    case Acoustics::FITKAcousticsNCTAbstractLayObj::Resistive://阻尼
    {
        ResistiveP = dynamic_cast<Acoustics::FITKAcousticsNCTResistive*>(lobj);
        break;
    }
    default:
    {
        return false;
        break;
    }
    }

    if (RegularTrimLayerP != nullptr)//缝隙 实体板 纤维 泡沫
    {
        int FluidMaterialID = RegularTrimLayerP->getFluidMaterialID();//流体id
        int SolidMaterialID = RegularTrimLayerP->getSolidMaterialID();//固体id
        double Thickness = RegularTrimLayerP->getThickness();//厚度
        double LossFactor = RegularTrimLayerP->getLossFactor();//损耗系数

        int ID = RegularTrimLayerP->getDataObjectID();
        m_toolp->writeGroupAttrInt(subGroup, "ID", ID);
        m_toolp->writeGroupAttrInt(subGroup, "FluidMaterialID", FluidMaterialID);
        m_toolp->writeGroupAttrInt(subGroup, "SolidMaterialID", SolidMaterialID);
        m_toolp->writeGroupAttrDouble(subGroup, "Thickness", Thickness);
        m_toolp->writeGroupAttrDouble(subGroup, "LossFactor", LossFactor);
        return true;
    }
    if (ResistiveP != nullptr)//阻尼
    {

        double Density = ResistiveP->getDensity();//密度
        double FlowResistivity = ResistiveP->getFlowResistivity();//流阻率
        double Thickness=ResistiveP->getThickness();//厚度
        m_toolp->writeGroupAttrDouble(subGroup, "Density", Density);
        m_toolp->writeGroupAttrDouble(subGroup, "Thickness", Thickness);
        m_toolp->writeGroupAttrDouble(subGroup, "FlowResistivity", FlowResistivity);
        return true;
    }
    if (SeptumP != nullptr)//隔离板
    {
        double SurfaceMass = SeptumP->getSurfaceMass();//表面质量
        double Thickness=SeptumP->getThickness();//厚度

        m_toolp->writeGroupAttrDouble(subGroup, "SurfaceMass", SurfaceMass);
        m_toolp->writeGroupAttrDouble(subGroup, "Thickness", Thickness);
        return true;

    }
    if (PerforatedP != nullptr)//多孔介质
    {
        double Density = PerforatedP->getDensity();//密度
        int fluidMaterialID = PerforatedP->getfluidMaterialID();//流体材料ID
        int MaterialID = PerforatedP->getMaterialID();//材料ID
        double Porosity = PerforatedP->getPorosity();//孔隙度
        double Radius = PerforatedP->getRadius();//半径
        double AreaRatio = PerforatedP->getAreaRatio();//面积比率
        double FlowResistivity = PerforatedP->getFlowResistivity();//流量阻力
        double Thickness=PerforatedP->getThickness();//厚度

        Acoustics::FITKAcousticsNCTPerforated::NCTSolidType SolidType = PerforatedP->getSolidType();//固体类型(枚举转换)
        QString SolidTypeStr = this->NCTSolidTypeToStr(SolidType);

        Acoustics::FITKAcousticsNCTPerforated::PerforationType PerforationType = PerforatedP->getPerforationType();//穿孔类型(枚举转换)
        QString PerforationTypeStr = this->PerforationToStr(PerforationType);

        m_toolp->writeGroupAttrDouble(subGroup, "Density", Density);
        m_toolp->writeGroupAttrInt(subGroup, "fluidMaterialID", fluidMaterialID);
        m_toolp->writeGroupAttrInt(subGroup, "MaterialID", MaterialID);
        m_toolp->writeGroupAttrDouble(subGroup, "Porosity", Porosity);
        m_toolp->writeGroupAttrDouble(subGroup, "Radius", Radius);
        m_toolp->writeGroupAttrDouble(subGroup, "AreaRatio", AreaRatio);
        m_toolp->writeGroupAttrDouble(subGroup, "FlowResistivity", FlowResistivity);
        m_toolp->writeGroupAttrDouble(subGroup, "Thickness", Thickness);
        m_toolp->writeGroupAttrStr(subGroup, "SolidType", SolidTypeStr.toStdString());
        m_toolp->writeGroupAttrStr(subGroup, "PerforationType", PerforationTypeStr.toStdString());
        return true;
    }
    return false;
}

QString IO::FITKAcousticsNCTMgrAdaptor::PerforationToStr(Acoustics::FITKAcousticsNCTPerforated::PerforationType type)
{
    switch (type)
    {
    case Acoustics::FITKAcousticsNCTPerforated::None:
    {
        return "None";
        break;
    }

    case Acoustics::FITKAcousticsNCTPerforated::Geometry:
    {
        return "Geometry";
        break;
    }

    case Acoustics::FITKAcousticsNCTPerforated::BiotProperty:
    {
        return "BiotProperty";
        break;
    }

    default:
    {
        return "None";
        break;
    }

    }
    return "None";
}

Acoustics::FITKAcousticsNCTPerforated::PerforationType IO::FITKAcousticsNCTMgrAdaptor::StrFromPerforation(QString type)
{
    if (type.size() == 0)
    {
        return Acoustics::FITKAcousticsNCTPerforated::PerforationType::None;
    }if (type == "Geometry")
    {
        return Acoustics::FITKAcousticsNCTPerforated::PerforationType::Geometry;
    }
    else if (type == "BiotProperty")
    {
        return Acoustics::FITKAcousticsNCTPerforated::PerforationType::BiotProperty;
    }
    return Acoustics::FITKAcousticsNCTPerforated::PerforationType::None;
}

QString IO::FITKAcousticsNCTMgrAdaptor::NCTLayUpTypeToStr(Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType type)
{

    switch (type)
    {
    case Acoustics::FITKAcousticsNCTAbstractLayObj::None:
    {
        return "None";
        break;
    }
    case Acoustics::FITKAcousticsNCTAbstractLayObj::Foam:
    {
        return "Foam";
        break;
    }
    case Acoustics::FITKAcousticsNCTAbstractLayObj::Fiber:
    {
        return "Fiber";
        break;
    }
    case Acoustics::FITKAcousticsNCTAbstractLayObj::Gap:
    {
        return "Gap";
        break;
    }
    case Acoustics::FITKAcousticsNCTAbstractLayObj::Panel:
    {
        return "Panel";
        break;
    }
    case Acoustics::FITKAcousticsNCTAbstractLayObj::Septum:
    {
        return "Septum";
        break;
    }
    case Acoustics::FITKAcousticsNCTAbstractLayObj::Perforated:
    {
        return "Perforated";
        break;
    }
    case Acoustics::FITKAcousticsNCTAbstractLayObj::Resistive:
    {
        return "Resistive";
        break;
    }
    default:
    {
        return "None";
        break;
    }
    }
    return "None";
}

Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType IO::FITKAcousticsNCTMgrAdaptor::StrFromNCTLayUpType(QString type)
{
    if (type == "None")
    {
        return Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType::None;
    }
    else if (type == "Foam")
    {
        return Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType::Foam;
    }
    else if (type == "Fiber")
    {
        return Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType::Fiber;
    }
    else if (type == "Gap")
    {
        return Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType::Gap;
    }
    else if (type == "Panel")
    {
        return Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType::Panel;
    }
    else if (type == "Septum")
    {
        return Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType::Septum;
    }
    else if (type == "Perforated")
    {
        return Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType::Perforated;
    }
    else if (type == "Resistive")
    {
        return Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType::Resistive;
    }
    else
    {
        return Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType::None;
    }
}

QString IO::FITKAcousticsNCTMgrAdaptor::NCTSolidTypeToStr(Acoustics::FITKAcousticsNCTPerforated::NCTSolidType type)
{
    switch (type)
    {
    case Acoustics::FITKAcousticsNCTPerforated::NoneSolid:
    {
        return "NoneSolid";
        break;
    }

    case Acoustics::FITKAcousticsNCTPerforated::Rigid:
    {
        return "Rigid";
        break;
    }

    case Acoustics::FITKAcousticsNCTPerforated::LimpDensity:
    {
        return "LimpDensity";
        break;
    }

    case Acoustics::FITKAcousticsNCTPerforated::FoamOrFiber:
    {
        return "FoamOrFiber";
        break;
    }

    default:
        return "NoneSolid";
        break;
    }
    return "NoneSolid";
}

Acoustics::FITKAcousticsNCTPerforated::NCTSolidType IO::FITKAcousticsNCTMgrAdaptor::StrFromNCTSolidType(QString type)
{
    if (type.size() == 0)
    {
        return Acoustics::FITKAcousticsNCTPerforated::NCTSolidType::NoneSolid;
    }
    else if (type == "NoneSolid")
    {
        return Acoustics::FITKAcousticsNCTPerforated::NCTSolidType::NoneSolid;
    }
    else if (type == "Rigid")
    {
        return Acoustics::FITKAcousticsNCTPerforated::NCTSolidType::Rigid;
    }
    else if (type == "LimpDensity")
    {
        return Acoustics::FITKAcousticsNCTPerforated::NCTSolidType::LimpDensity;
    }
    else if (type == "FoamOrFiber")
    {
        return Acoustics::FITKAcousticsNCTPerforated::NCTSolidType::FoamOrFiber;
    }
    return Acoustics::FITKAcousticsNCTPerforated::NCTSolidType::NoneSolid;
}

